package com.aaa.power.controller;

import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.power.entity.Role;
import com.aaa.power.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 角色信息表(Role)表控制层
 *
 * @author makejava
 * @since 2020-11-10 13:52:31
 */
@RestController
@RequestMapping("role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 通过主键该角色所有权限
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        //当ID空为时抛出自定义异常
        if (null == id) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.roleService.queryById(id));
    }
    @GetMapping("queryPageByParam")
    public Result queryPageByParam(Integer pageNo, Integer pageSize, Role role){
        return roleService.queryPageByParam(pageNo,pageSize,role);
    }

    /**
     * 根据角色id查询该角色所有的权限
     * @param roleId
     * @return
     */
    @GetMapping("queryByIdToMenu")
    public Result queryByIdToMenu(Integer roleId){
        //当ID空为时抛出自定义异常
        if (null == roleId) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.roleService.queryByIdToMenu(roleId));
    }

    /**
     * 根据角色id和菜单id给角色分配权限
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("insertByIdToMenus")
    public Result insertByIdToMenus(@RequestParam("roleId") Integer roleId,@RequestParam("menuIds") String menuIds){
        System.out.println(roleId+"klfgjfkldj");
        //当ID空为时抛出自定义异常
        if (null == roleId) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.roleService.insertByIdToMenus(roleId,menuIds));
    }
    /**
     * 添加系统角色
     * @param role
     * @return
     */
    @PostMapping("insert")
    public Result inset(@RequestBody Role role){
        //判断新增的角色名字为空抛出异常
        if (role.getRoleName()==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }else{
            //返回成功
            return new Result(ReturnCode.SUCCESSS.getCode(),
                    ReturnCode.SUCCESSS.getMsg(), this.roleService.insert(role));
        }
    }

    /**
     * 根据角色id修改角色信息
     * @param role
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody  Role role){
        //当ID空为时抛出自定义异常
        if (role.getRoleId()==null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.roleService.update(role));
    }

    /**
     * 根据角色id删除角色信息
     * @param roleId
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer roleId){
        //当ID空为时抛出自定义异常
        if (null == roleId) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.roleService.deleteById(roleId));
    }
    /**
     * 查询所有
     * @param
     * @return
     */
    @GetMapping("queryAll")
    public Result queryAll(){
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.roleService.queryAll(null));
    }
}